// pages/results/results.js
var app = getApp();
Page({
  data: {
    modalShow:false,
    index:0,
    time:"",
    totalScore: null, // 分数
    wrongList: [], // 错误的题数
    chooseValue: [], // 选择的答案
    questionList:[],
    remark: ["好极了！你很棒棒哦", "哎哟不错哦", "别灰心，继续努力哦！"], // 评语
  },
  onLoad: function (options) {
    console.log(options);
    wx.setNavigationBarTitle({ title: options.testId }) // 动态设置导航条标题
    let wrongList = JSON.parse(options.wrongList);
    let chooseValue = JSON.parse(options.chooseValue);
    let questionList = JSON.parse(options.questionList);
    this.setData({
      totalScore: options.totalScore,
      time: options.time,
      wrongList: wrongList,
      chooseValue: chooseValue,
      questionList:questionList
    })
    console.log(this.data.chooseValue);
  },
  //上传数据
  onUnload:function()
  {
    let that=this
    let openid=wx.getStorageSync('openid');
    let test=[]
    let testItem={}
    testItem.name="安神药"
    testItem.testId=that.data.questionList.testId
    testItem.chooseValue=that.data.chooseValue
    testItem.wrongList=that.data.wrongList
    testItem.score=that.data.totalScore
    testItem.time=that.data.time
    test = Object.assign({},testItem);
    // test=that.data.questionList.test
    console.log(test)
    wx.cloud.callFunction({
      name:"updata",
      data:{
        action:"userList_test_update",           
        openid:openid,
        test:test
      }
    })
  },
  // 查看错题
  toView: function () {
    // this.setData({
    //   modalShow:!this.data.modalShow
    // })
    wx.navigateTo({
      url: '../testView/testView',
    })
  },
  // 返回首页
  toIndex: function () {
    wx.redirectTo({
      url: '../home/home',
    })
  },
  previous()
  {
    if (this.data.index > 0) {
    this.setData({
      index: this.data.index - 1
    })
  }
  },
  nextSubmit()
  {
    if (this.data.index < this.data.questionList.length - 1) {
      // 渲染下一题
      this.setData({
        index: this.data.index + 1
      })
    }
    else
    {
      this.setData({
        modalShow:!this.data.modalShow
      })
    }
  },
})